package com.base.cn.platform.os.service.op.affiche.tmp;

import com.base.cn.platform.os.common.msg.WXTmpEnum;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.entity.agent.user.AgentUser;
import com.base.cn.platform.os.entity.op.affiche.OpReceiveAffiche;
import com.base.cn.platform.os.entity.user.user.CusUser;
import com.base.cn.platform.os.entity.user.user.CuserProfile;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 推广员审核消息通知
 *
 * @author s.li
 * @date 2019/05/29
 */
@Component
public class AgentSendTemAffice extends  SendTemAffice{

    @Override
    public Map<String, Object> initMsg(Object[] arr, WXTmpEnum tmpEnum, List<CusUser> userList, Map<BigDecimal, List> dataListMap) {
        initService(arr);

        StringBuilder varNum1 = new StringBuilder(smsTempConfigMap.get("agentSuccessVarNum1"));
        StringBuilder varNum2 = new StringBuilder(smsTempConfigMap.get("agentSuccessVarNum2"));
        StringBuilder agentRemark = new StringBuilder(smsTempConfigMap.get("agentSuccessRemark"));
        StringBuilder agentMsgType = new StringBuilder(smsTempConfigMap.get("agentSuccessMsgType"));
        if(tmpEnum.getKey().equals(WXTmpEnum.AGENT_FAIL.getKey())){
            varNum1.delete(0,varNum1.length());
            varNum2.delete(0,varNum2.length());
            agentRemark.delete(0,agentRemark.length());
            agentMsgType.delete(0,agentMsgType.length());
            varNum1.append(smsTempConfigMap.get("agentErrorVarNum1"));
            varNum2.append(smsTempConfigMap.get("agentErrorVarNum2"));
            agentRemark.append(smsTempConfigMap.get("agentErrorRemark"));
            agentMsgType.append(smsTempConfigMap.get("agentErrorMsgType"));
        }

        if(StringUtils.isNotEmpty(varNum1)
                && StringUtils.isNotEmpty(varNum2)
                && StringUtils.isNotEmpty(agentMsgType)){
            AgentUser agentUser = (AgentUser)dataListMap.get(userList.get(0).getId()).get(0);
            Map<String,Object> msg = new HashMap<>();
            boolean status = false;
            if(agentMsgType.toString().contains(MsgType.SITE.getKey())){
                List<OpReceiveAffiche> receiveAfficheList = new ArrayList<>();
                userList.forEach(user -> {
                    OpReceiveAffiche affiche = new OpReceiveAffiche();
                    affiche.setMsgType(1);
                    affiche.setStatus(1);
                    affiche.setUserId(user.getId());
                    StringBuilder examContext = new StringBuilder();
                    examContext.append(varNum1.toString().replace("{1}",agentUser.getAgentUserName()));
                    examContext.append(" , ");
                    examContext.append(varNum2.toString().replace("{2}", DateUtils.format(agentUser.getUpdateTime(),"yyyy年MM月dd日 HH:mm")));
                    if(StringUtils.isNotEmpty(agentRemark)){
                        examContext.append(" , ");
                        examContext.append(agentRemark);
                    }
                    affiche.setContext(examContext.toString());
                    receiveAfficheList.add(affiche);
                });
                msg.put(SITE_MESSAGE_KEY,receiveAfficheList);
                status = true;
            }
            if(agentMsgType.toString().contains(MsgType.WX.getKey())){
                List<CuserProfile> cuserProfileList =this.getCuserProfileList(userList);
                if(ObjectUtils.isNotEmpty(cuserProfileList)){
                    String tmpId = getTmpId(tmpEnum);
                    List<JSONObject> jsonObjectList = new ArrayList<>();
                    cuserProfileList.forEach(cuserProfile -> {
                        //设置参数
                        JSONObject obj = new JSONObject();
                        obj.put("touser", cuserProfile.getOpenId());
                        obj.put("template_id", tmpId);
                        //微信消息数据内容
                        Map<String,Map<String,String>> data = new HashMap<>();

                        //消息标题
                        Map<String,String> first = new HashMap<>();
                        first.put("value","推广员申请成功通知");
                        if(tmpEnum.getKey().equals(WXTmpEnum.AGENT_FAIL.getKey())){
                            first.put("value","推广员申请未通过通知");
                        }
                        first.put("color","#173177");
                        data.put("first",first);

                        Map<String,String> keyword1 = new HashMap<>();
                        keyword1.put("value",agentUser.getAgentUserName());
                        keyword1.put("color","#173177");
                        data.put("keyword1",keyword1);

                        Map<String,String> keyword2 = new HashMap<>();
                        keyword2.put("value",  DateUtils.format(agentUser.getUpdateTime(),"yyyy年MM月dd日 HH:mm"));
                        keyword2.put("color","#173177");
                        data.put("keyword2",keyword2);
                        this.setRemarkMap(data,agentRemark.toString());
                        obj.put("data", data);
                        jsonObjectList.add(obj);
                    });
                    msg.put(WX_MESSAGE_KEY,jsonObjectList);
                    status = true;
                }
            }
            if(status){
                msg.put(MSG_TYPE,agentMsgType.toString());
                return msg;
            }
        }
        return null;
    }
}
